package com.example.day2.busi.goods;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.example.day2.R;
import com.example.day2.adapter.GoodsAdapter;
import com.example.day2.adapter.GoodsTypeAdapter;
import com.example.day2.adapter.SmallTypeAdapter;
import com.example.day2.databinding.FragmentShopBinding;
import com.example.day2.entity.GoodsResult;
import com.example.day2.entity.GoodsTableResult;
import com.example.day2.mvvm.base.BaseFragment;
import com.example.day2.mvvm.base.BaseViewFragment;

import java.util.ArrayList;
import java.util.List;

public class ShopFragment extends BaseFragment<FragmentShopBinding,GoodsViewModel> {
    private GoodsTypeAdapter goodsTypeAdapter = new GoodsTypeAdapter(R.layout.item_goods_type);
    private SmallTypeAdapter smallTypeAdapter = new SmallTypeAdapter(R.layout.item_goods_small_type);
    private GoodsAdapter goodsAdapter = new GoodsAdapter(R.layout.item_goods);
    private List<GoodsResult.DataBean> list =new ArrayList<>();
    private int page = 1;
    @Override
    public void initView() {

        binding.rv1.setAdapter(goodsTypeAdapter);
        binding.rv1.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.HORIZONTAL,false));

        binding.rv2.setLayoutManager(new GridLayoutManager(getContext(),5));
        binding.rv2.setAdapter(smallTypeAdapter);

        binding.rv3.setLayoutManager(new GridLayoutManager(getContext(),2));
        binding.rv3.setAdapter(goodsAdapter);

        //点击一级分类，切换二级分类
        goodsTypeAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                GoodsTableResult.DataBean dataBean = goodsTypeAdapter.getData().get(position);
                smallTypeAdapter.getData().clear();
                smallTypeAdapter.getData().addAll(dataBean.getData());
                smallTypeAdapter.notifyDataSetChanged();


            }
        });
        //点击二级分类，切换商品
        smallTypeAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                GoodsTableResult.DataBean.DataBean1 dataBean1 = smallTypeAdapter.getData().get(position);
                viewModel.getGoods(dataBean1.getId(),1,10);


            }
        });
        //搜索
        binding.et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String s1 = binding.et.getText().toString();
                List<GoodsResult.DataBean> list1 = new ArrayList<>();

                for (GoodsResult.DataBean datum : list) {
                    if (datum.getGoods_desc().contains(s1)){
                        list1.add(datum);
                    }
                }
                goodsAdapter.getData().clear();
                goodsAdapter.getData().addAll(list1);
                goodsAdapter.notifyDataSetChanged();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

    @Override
    public void initData() {
        viewModel.getGoodsType();
        //监听分类
        viewModel.successType.observe(this, new Observer<GoodsTableResult>() {
            @Override
            public void onChanged(GoodsTableResult goodsTableResult) {
                if (goodsTableResult.getCode() == 200){

                    //处理一级列表类型
                    goodsTypeAdapter.getData().addAll(goodsTableResult.getData());
                    goodsTypeAdapter.notifyDataSetChanged();
                    //获取第0个下标的一级分类作为默认的二级数据
                    GoodsTableResult.DataBean dataBean = goodsTableResult.getData().get(0);
                    List<GoodsTableResult.DataBean.DataBean1> data = dataBean.getData();
                    smallTypeAdapter.getData().addAll(data);
                    smallTypeAdapter.notifyDataSetChanged();

                    //获取二级分类的第0个下标，作为默认的商品列表分类数据
                    int id = data.get(0).getId();
                    viewModel.getGoods(id,page,10);
                }
            }
        });
        //监听商品列表
        viewModel.success.observe(this, new Observer<GoodsResult>() {
            @Override
            public void onChanged(GoodsResult goodsResult) {
                if (goodsResult.getCode() == 200){
                    goodsAdapter.getData().clear();
                    list.clear();
                    list.addAll(goodsResult.getData());
                    goodsAdapter.getData().addAll(goodsResult.getData());
                    goodsAdapter.notifyDataSetChanged();
                }
            }
        });

    }

    @Override
    public Class<GoodsViewModel> getVIewModel() {
        return GoodsViewModel.class;
    }

    @Override
    public int bindLayoutId() {
        return R.layout.fragment_shop;
    }
}
